package properties;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA
 * Description:Properties学习
 * User:22954
 * Date:2024-03-28
 * Time:21:35
 */
@SuppressWarnings("all")
public class Properties_ {
    /*
    Properties类能便捷读取配置文件
    配置文件:内容包括一系列数据等,供程序读取,外部修改配置文件数据时,不需要对程序源码进行改动
     */
    @Test
    public void read() throws IOException {//读取配置文件----load()加载文件数据到对象----list()将对象中的数据输出到指定流中
        Properties properties = new Properties();
        properties.load(new FileReader("src/properties/mysql.properties"));
        properties.list(System.out);
    }
    @Test
    public void add() throws IOException {//写入配置文件(没有文件会创建新文件,已有文件会覆盖原文件)----setProperties()输入键值对----store()将对象中数据写入输出流路径中
        Properties properties = new Properties();
        properties.setProperty("ip","192.135.200.111");
        properties.setProperty("user","汤姆");
        properties.setProperty("pwd","999");
        properties.store(new FileWriter("src/properties/mysql.properties"),null);
    }
    @Test
    public void modify() throws IOException {//修改配置文件----load()加载原数据至对象----setProperties()相同key覆盖value----store()
        Properties properties = new Properties();
        properties.load(new FileReader("src/properties/mysql.properties"));
        properties.setProperty("pwd","123");
        properties.store(new FileWriter("src/properties/mysql.properties"),"COMMENT");
    }
}
